#ifndef SERVERMANAGER_H
#define SERVERMANAGER_H

#include <QObject>
#include <QMap>
#include <QTcpServer>
#include <QTcpSocket>
#include "clienthandelr.h"

class serverManager : public QObject
{
    Q_OBJECT
public:
    explicit serverManager(QObject *parent = nullptr);
    explicit serverManager(QString ip, int port, QObject *parent = nullptr);

    QString getIp() const;
    void setIp(const QString &value);

    int getPort() const;
    void setPort(int value);

    bool start_listen();

    bool isUserOnline(const QString& username) const;
    QTcpSocket* getUserSocket(const QString& username) const;

private:
    QTcpServer* server = nullptr;
    QString ip = "";
    int port = -1;
    QMap<QTcpSocket*,clientHandelr*> clients;
    QMap<QString,QTcpSocket*> onl_users;
    void initServer();
private slots:
    void onNewConnection();
    void onNewOnlUser(QString name,QTcpSocket* socket);
    void onOnlOff(QString name);
    void onclient_upList(QTcpSocket* socket);//处理客户端 更新列表请求
    void on_singleMessage(MessagePackage& pack);//转发私聊
signals:
    void new_client(clientHandelr* handler);
    void new_onlUser(QString name);
    void onlUserOff(QString name);//在线用户列表删除下线用户

};

#endif // SERVERMANAGER_H
